package com.ele.dao.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.ele.dao.FoodDao;
import com.ele.pojo.Food;
import com.ele.utils.JDBCUtils;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class FoodDaoImpl implements FoodDao {
    private DataSource dataSource = JDBCUtils.getDatasource();
    @Override
    public List<Food> findFoodList(String currentPage, String pageSize, String categoryId, String shopId, String foodName) throws SQLException {
        String sql = "select * from food where shop_id = " + shopId;
        Map<String, String> map = new HashMap<>();
        if (StrUtil.isNotBlank(categoryId)) {
            map.put("category_id", categoryId);
        }
        if (StrUtil.isNotBlank(foodName)) {
            map.put("food_name", foodName);
        }
        // 处理map集合
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        for (Map.Entry<String, String> entry : entrySet) {
            if (entry.getKey().equals("food_name")) {
                sql = sql + " and " + entry.getKey() + " like  '%" + entry.getValue() + "%' ";
            } else {
                sql = sql + " and " + entry.getKey() + " = " + entry.getValue();
            }
        }
        System.out.println(sql);
        Integer page = 1;
        Integer size = 5;
        if (StrUtil.isNotBlank(currentPage)) {
            page = Integer.parseInt(currentPage);
        }
        if (StrUtil.isNotBlank(pageSize)) {
            size = Integer.valueOf(pageSize);
        }
        page = (page - 1) * size;
        sql = sql + " limit " + page + " , " + size;
        System.out.println(sql);
        List<Food> query = Db.use(dataSource).query(sql, Food.class);
        return query;
    }
    @Override
    public Long foodCount(String shopId) throws SQLException {
        return Db.use(dataSource).count(Entity.create("food").set("shop_id", shopId));
    }

    @Override
    public Integer addFood(Food food) throws SQLException {
        return Db.use(dataSource).insert(Entity.create("food")
                .set("food_name", food.getFoodName())
                .set("food_explain", food.getFoodExplain())
                .set("food_img", food.getFoodImg())
                .set("food_price", food.getFoodPrice())
                .set("shop_id", food.getShopId())
                .set("remarks", food.getRemarks())
                .set("stock", food.getStock())
                .set("status", 0)
                .set("category_id", food.getCategoryId()));
    }

    @Override
    public Integer updateFood(Food food) throws SQLException {
        return Db.use(dataSource).update(
                Entity.create()
                        .set("food_name", food.getFoodName())
                        .set("food_explain", food.getFoodExplain())
                        .set("food_img", food.getFoodImg())
                        .set("food_price", food.getFoodPrice())
                        .set("remarks", food.getRemarks())
                        .set("status", food.getStatus())
                        .set("stock", food.getStock()),
                Entity.create("food").set("food_id", food.getFoodId()));
    }

    @Override
    public Integer deleteFood(String foodId) throws SQLException {
        return Db.use(dataSource).del("food","food_id",foodId);
    }
}
